//
//  UIIntroduceController.m
//  WaterStreet
//
//  Created by 재효 조 on 12. 2. 13..
//  Copyright (c) 2012년 가족. All rights reserved.
//

#import "UIIntroduceController.h"

@implementation UIIntroduceController
@synthesize lblStatus= _label;

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    //UIScrollView * scrollview = (UIScrollView *) self.view;
    
    NSLog(@"여기는 찍히냐?");
    
    NSLog(@"%f", self.view.frame.size.width);
    
    
//    scrollview.contentSize = CGSizeMake(self.view.frame.size.width+300, self.view.frame.size.height);
}
- (IBAction)bumpTest:(id)sender {
    BumpAPI *api = [BumpAPI sharedInstance];
    // 6ae27f01730c42cd90e7cfc1cf156036
	[api configAPIKey:@"6ae27f01730c42cd90e7cfc1cf156036"];
	[api configUIDelegate:self];
	[api configDelegate:self];
	[api requestSession];
}


/**
 * Result of requestSession on BumpAPI (user wants to connect to another device via Bump). UI should
 * now first appear saying something like "Warming up".
 */
-(void)bumpRequestSessionCalled {
	[_label setText:@"Request Session Called... Warming up"];
}

/**
 * We were unable to establish a connection to the Bump network. Either show an error message or
 * hide the popup. The BumpAPIDelegate is about to be called with bumpSessionFailedToStart.
 */
-(void)bumpFailedToConnectToBumpNetwork {
	[_label setText:@"Failed to connect... retrying"];
	[[BumpAPI sharedInstance] requestSession];
}

/**
 * We were able to establish a connection to the Bump network and you are now ready to bump. 
 * The UI should say something like "Ready to Bump".
 */
-(void)bumpConnectedToBumpNetwork {
	[_label setText:@"Ready to Bump"];
}

/**
 * Result of endSession call on BumpAPI. Will soon be followed by the call to bumpSessionEnded: on
 * API delegate. Highly unlikely to happen while the custom UI is up, but provided as a convenience
 * just in case.
 */
-(void)bumpEndSessionCalled {
	[_label	 setText:@"End Session was called"];
	NSLog(@"UI Callback, end session called");
}

/**
 * Once the intial connection to the bump network has been made, there is a chance the connection
 * to the Bump Network is severed. In this case the bump network might come back, so it's
 * best to put the user back in the warming up state. If this happens too often then you can 
 * provide extra messaging and/or explicitly call endSession on the BumpAPI.
 */
-(void)bumpNetworkLost {
	[_label setText:@"Warming up... network was lost"];
}

/**
 * Physical bump occurced. Update UI to tell user that a bump has occured and the Bump System is
 * trying to figure out who it matched with.
 */
-(void)bumpOccurred {
	[_label setText:@"Bumped! Trying to connecect..."];
}

/**
 * Let's you know that a match could not be made via a bump. It's best to prompt users to try again.
 * @param		reason			Why the match failed
 */
-(void)bumpMatchFailedReason:(BumpMatchFailedReason)reason {
	[_label setText:@"Match failed, try again"];
}

/**
 * The user should be presented with some data about who they matched, and whether they want to
 * accept this connection. (Pressing Yes/No should call confirmMatch:(BOOL) on the BumpAPI).
 * param		bumper			Information about the device the bump system mached with
 */
-(void)bumpMatched:(Bumper*)bumper {
	[_label setText:@"Matched! about to start..."];
	[[BumpAPI sharedInstance] confirmMatch:YES];
}

/**
 * Called after both parties have pressed yes, and bumpSessionStartedWith:(Bumper) is about to be 
 * called on the API Delegate. You should now close the matching UI.
 */
-(void)bumpSessionStarted {
	[_label setText:@"Session started!!!"];
}


#pragma mark *********************** API Delegate

/**
 Successfully started a Bump session with another device.
 @param		otherBumper		Let's you know how the other device identifies itself
 Can also be accessed later via the otherBumper method on the API
 */
- (void) bumpSessionStartedWith:(Bumper *)otherBumper {
	//Start sending data
	//call end session when you're done sending stuff you want
    NSLog(@"start sending data");
    BumpAPI *api = [BumpAPI sharedInstance];
    
    
    NSMutableDictionary *data = [[NSMutableDictionary alloc]init ];
    [data setValue:@"조재효" forKey:@"name"];
    
    NSData * nsdata =  [NSKeyedArchiver archivedDataWithRootObject:data];
    
    [api sendData:nsdata];
    
//    otherBumper
    
}

/**
 There was an error while trying to start the session these reasons are helpful and let you know
 what's going on
 @param		reason			Why the session failed to start
 */
- (void) bumpSessionFailedToStart:(BumpSessionStartFailedReason)reason {
	
}

/**
 The bump session was ended, reason tells you wheter it was expected or not
 @param		reason			Why the session ended. Could be either expected or unexpected.
 */
- (void) bumpSessionEnded:(BumpSessionEndReason)reason {
	[_label setText:@"Bump session ended"];
	NSLog(@"Session has ended, requesting a new one");
	[[BumpAPI sharedInstance] requestSession];//auto request a new session since we always want
    //to be doing something
}

/**
 The symmetrical call to sendData on the API. When the other device conneced via Bump calls sendData
 this device get's this call back
 @param		reason			Data sent by the other device.
 */
- (void) bumpDataReceived:(NSData *)chunk {
	//start recieving data
    NSLog(@"start recieving data");
    
    NSMutableDictionary *data = [NSKeyedUnarchiver unarchiveObjectWithData:chunk];
    
    [_label setText:[data valueForKey:@"name"]];
    
    BumpAPI *api = [BumpAPI sharedInstance];
    
    NSMutableDictionary *resultData = [[NSMutableDictionary alloc]init ];
    [data setValue:[[data valueForKey:@"name"] stringByAppendingString:@" resend" ]  forKey:@"name"];
    
    NSData * nsdata =  [NSKeyedArchiver archivedDataWithRootObject:resultData];
    
    [api sendData:nsdata];
    
}

- (void)viewDidUnload {
    [self setLblStatus:nil];
    [super viewDidUnload];
}
@end
